//
//  addViewController.m
//  08-HomeWork
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "addViewController.h"
#import "ViewController.h"

@interface addViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textField_name;
@property (nonatomic, strong) UITextField *textField_gender;
@property (nonatomic, strong) UITextField *textField_age;
@property (nonatomic, strong) UITextField *textField_number;
@end

@implementation addViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"添加成员";
    [self createTextField];
    [self createButton];
}
- (void)createTextField {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"welcome_2"];
    imageView.alpha = 0.8;
    [self.view addSubview:imageView];
    self.textField_name = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.textField_gender = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 200, 40)];
    self.textField_age = [[UITextField alloc] initWithFrame:CGRectMake(100, 220, 200, 40)];
    self.textField_number = [[UITextField alloc] initWithFrame:CGRectMake(100, 280, 200, 40)];
    _textField_name.borderStyle = UITextBorderStyleBezel;
    _textField_gender.borderStyle = UITextBorderStyleBezel;
    _textField_age.borderStyle = UITextBorderStyleBezel;
    _textField_number.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:_textField_name];
    [self.view addSubview:_textField_number];
    [self.view addSubview:_textField_gender];
    [self.view addSubview:_textField_age];
    
    UILabel *label_name = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 80, 40)];
    UILabel *label_gender = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, 80, 40)];
    UILabel *label_age = [[UILabel alloc] initWithFrame:CGRectMake(0, 220, 80, 40)];
    UILabel *label_number = [[UILabel alloc] initWithFrame:CGRectMake(0, 280, 80, 40)];
    [self.view addSubview:label_age];
    [self.view addSubview:label_gender];
    [self.view addSubview:label_name];
    [self.view addSubview:label_number];
    label_name.text = @"姓 名:";
    label_gender.text = @"性 别:";
    label_age.text = @"年 龄:";
    label_number.text = @"电话号码:";
    label_name.textAlignment = NSTextAlignmentRight;
    label_age.textAlignment = NSTextAlignmentRight;
    label_gender.textAlignment = NSTextAlignmentRight;
    label_number.textAlignment = NSTextAlignmentRight;
    _textField_number.delegate = self;
    _textField_age.delegate = self;
    _textField_gender.delegate = self;
    _textField_name.delegate = self;

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)createButton {
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
    saveButton.frame = CGRectMake(100, 350, 200, 40);
    [self.view addSubview:saveButton];
    [saveButton addTarget:self action:@selector(handleSave:) forControlEvents:UIControlEventTouchUpInside];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:20];
    saveButton.tintColor = [UIColor whiteColor];
    saveButton.layer.cornerRadius = 5;
    saveButton.backgroundColor = [UIColor colorWithRed:0.110 green:0.624 blue:0.447 alpha:1.000];
}
- (void)handleSave:(UIButton *)button {
    
   if (self.delegate && [self.delegate respondsToSelector:@selector(informationAddDictionary:)]){
        [self.delegate informationAddDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:_textField_name.text, @"name", _textField_gender.text, @"gender", [[[NSNumberFormatter alloc] init] numberFromString:_textField_age.text], @"age", _textField_number.text, @"phoneNumber", nil]];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
